﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for AdministracijaUtilityMethods
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
 [System.Web.Script.Services.ScriptService]
public class AdministracijaUtilityMethods : System.Web.Services.WebService {

    public AdministracijaUtilityMethods () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string ClanakDrvo(string drvoid) {
        EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1 entiteti = new EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1();
        
        int id = Int32.Parse(drvoid);
        var cl = from c in entiteti.Drvo
                 where c.DrvoID == id
                 select c.Clanak;
        return cl.First();
    }

    [WebMethod]
    public string ClanakEditText(string clanakid)
    {
        EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1 entiteti = new EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1();

        int id = Int32.Parse(clanakid);
        var cl = from c in entiteti.Clanak
                 where c.ClanakID == id
                 select c.TextClanka;
        return cl.First();
    }

    [WebMethod]
    public string ClanakDrvoUpdate(string sadrzaj,string id)
    {
        EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1 entiteti = new EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1();
        int idd = Int32.Parse(id);
        try
        {
            var dr = from c in entiteti.Drvo
                     where c.DrvoID == idd
                     select c;

            dr.First().Clanak = sadrzaj;
            entiteti.SaveChanges();
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
        
        return "Podaci uspješno promijenjeni";
    }

    [WebMethod]
    public string ClanakUpdate(string sadrzaj, string id)
    {
        EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1 entiteti = new EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1();
        int idd = Int32.Parse(id);
        try
        {
            var dr = from c in entiteti.Clanak
                     where c.ClanakID == idd
                     select c;

            dr.First().TextClanka = sadrzaj;
            entiteti.SaveChanges();
        }
        catch (Exception ex)
        {
            return ex.Message;
        }

        return "Podaci uspješno promijenjeni";
    }
    
}
